package entities 
{
	import org.flixel.*;

	public class Player extends FlxSprite 
	{
		[Embed(source = "../../art/Robot.png")] private var gfxSprite:Class;
		
		public var score:Number = 0;
		public var discsHeld:Number = 4;
		
		public static const DRIVE_SPEED:Number = 400;
		public static const TURN_SPEED:Number = 200;
		
		public var animationPlaying:Boolean = false;
		public var shooting:Boolean = false;
		public var pickup:Boolean = false;
		
		public var discArray:Array;
		public var currentDisc:Number = 0;
		
		
		public function Player() 
		{
			super(FlxG.width / 2, FlxG.height / 2);
			
			loadGraphic(gfxSprite, true, false, 32, 64);
			addAnimation("0disc", [0]);
			addAnimation("1disc", [1]);
			addAnimation("2disc", [2]);
			addAnimation("3disc", [3]);
			addAnimation("4disc", [4]);
			addAnimation("pickup", [5, 6, 7, 6, 5], 10, false);
			addAnimation("shoot", [8, 9, 10, 9, 8], 10, false);
			play("4disc");
			
			antialiasing = true;
			
			drag.x = DRIVE_SPEED * 100;
			drag.y = DRIVE_SPEED * 100;
			maxVelocity.x = DRIVE_SPEED;
			maxVelocity.y = DRIVE_SPEED;
			
			angularDrag = TURN_SPEED * 8;
			maxAngular = TURN_SPEED;
			
			discsHeld = 4;
		}
		
		override public function update():void {
			acceleration.x = 0;
			acceleration.y = 0;
			angularAcceleration = 0;
			
			if (FlxG.keys.W || FlxG.keys.UP) {
				velocity.y = Math.cos(angle*(Math.PI/180))*DRIVE_SPEED*-1;
				velocity.x = Math.sin(angle*(Math.PI/180))*DRIVE_SPEED;
			}
			if (FlxG.keys.S || FlxG.keys.DOWN) {
				velocity.y = Math.cos(angle*(Math.PI/180))* (DRIVE_SPEED / 2);
				velocity.x = Math.sin(angle*(Math.PI/180))* (DRIVE_SPEED / 2) * -1;
			}
			if ((FlxG.keys.A || FlxG.keys.LEFT)) {
				angularAcceleration = -angularDrag;
			}
			if ((FlxG.keys.D || FlxG.keys.RIGHT)) {
				angularAcceleration = angularDrag;	
			}
			if (FlxG.keys.justPressed("SPACE")) {
				if (discsHeld > 0 && !shooting && !pickup) {
					ShootDisc(x, y, Math.sin(angle*(Math.PI/180))* (DRIVE_SPEED / 2), Math.cos(angle*(Math.PI/180))* (DRIVE_SPEED / 2)  * -1);
				}
			}
			
			y = FlxU.max(-16, y);
			y = FlxU.min(FlxG.height - 28, y);
			x = FlxU.max(-16, x);
			x = FlxU.min(FlxG.width -232, x);
			
			if (finished) {
				pickup = false;
				shooting = false;
				play(discsHeld.toString() + "disc");
			}
			
			super.update();
		}
		
		private function ShootDisc(_x:Number, _y:Number, xVel:Number, yVel:Number):void {
			play("shoot");
			shooting = true;
			
			discsHeld--;
			
			discArray[currentDisc].SendDisc(_x, _y, xVel, yVel);
			currentDisc++;
			if (currentDisc > discArray.length - 1) {
				currentDisc = 0;
			}
		}
		
	}

}